﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class SoDuDauKyBO : Csla.BusinessBase<SoDuDauKyBO>
    {
        private Hashtable hstOldValue = new Hashtable();
        public Hashtable HstOldValue
        {
            get { return this.hstOldValue; }
            set { this.hstOldValue = value; }
        }

        #region Business Properties

        private int? _SoDuDauKyID = null;
        public int? SoDuDauKyID
        {
            get { return this._SoDuDauKyID; }
            set { this._SoDuDauKyID = value; PropertyHasChanged("SoDuDauKyID"); }
        }

        private int? _KhachHangID = null;
        public int? KhachHangID
        {
            get { return this._KhachHangID; }
            set { this._KhachHangID = value; PropertyHasChanged("KhachHangID"); }
        }

        private int? _Nam = null;
        public int? Nam
        {
            get { return this._Nam; }
            set { this._Nam = value; PropertyHasChanged("Nam"); }
        }

        private double? _No1 = null;
        public double? No1
        {
            get { return this._No1; }
            set { this._No1 = value; PropertyHasChanged("No1"); }
        }

        private double? _Co1 = null;
        public double? Co1
        {
            get { return this._Co1; }
            set { this._Co1 = value; PropertyHasChanged("Co1"); }
        }

        private double? _No2 = null;
        public double? No2
        {
            get { return this._No2; }
            set { this._No2 = value; PropertyHasChanged("No2"); }
        }

        private double? _Co2 = null;
        public double? Co2
        {
            get { return this._Co2; }
            set { this._Co2 = value; PropertyHasChanged("Co2"); }
        }

        private double? _No3 = null;
        public double? No3
        {
            get { return this._No3; }
            set { this._No3 = value; PropertyHasChanged("No3"); }
        }

        private double? _Co3 = null;
        public double? Co3
        {
            get { return this._Co3; }
            set { this._Co3 = value; PropertyHasChanged("Co3"); }
        }

        private double? _No4 = null;
        public double? No4
        {
            get { return this._No4; }
            set { this._No4 = value; PropertyHasChanged("No4"); }
        }

        private double? _Co4 = null;
        public double? Co4
        {
            get { return this._Co4; }
            set { this._Co4 = value; PropertyHasChanged("Co4"); }
        }

        private double? _No5 = null;
        public double? No5
        {
            get { return this._No5; }
            set { this._No5 = value; PropertyHasChanged("No5"); }
        }

        private double? _Co5 = null;
        public double? Co5
        {
            get { return this._Co5; }
            set { this._Co5 = value; PropertyHasChanged("Co5"); }
        }

        private double? _No6 = null;
        public double? No6
        {
            get { return this._No6; }
            set { this._No6 = value; PropertyHasChanged("No6"); }
        }

        private double? _Co6 = null;
        public double? Co6
        {
            get { return this._Co6; }
            set { this._Co6 = value; PropertyHasChanged("Co6"); }
        }

        private double? _No7 = null;
        public double? No7
        {
            get { return this._No7; }
            set { this._No7 = value; PropertyHasChanged("No7"); }
        }

        private double? _Co7 = null;
        public double? Co7
        {
            get { return this._Co7; }
            set { this._Co7 = value; PropertyHasChanged("Co7"); }
        }

        private double? _No8 = null;
        public double? No8
        {
            get { return this._No8; }
            set { this._No8 = value; PropertyHasChanged("No8"); }
        }

        private double? _Co8 = null;
        public double? Co8
        {
            get { return this._Co8; }
            set { this._Co8 = value; PropertyHasChanged("Co8"); }
        }

        private double? _No9 = null;
        public double? No9
        {
            get { return this._No9; }
            set { this._No9 = value; PropertyHasChanged("No9"); }
        }

        private double? _Co9 = null;
        public double? Co9
        {
            get { return this._Co9; }
            set { this._Co9 = value; PropertyHasChanged("Co9"); }
        }

        private double? _No10 = null;
        public double? No10
        {
            get { return this._No10; }
            set { this._No10 = value; PropertyHasChanged("No10"); }
        }

        private double? _Co10 = null;
        public double? Co10
        {
            get { return this._Co10; }
            set { this._Co10 = value; PropertyHasChanged("Co10"); }
        }

        private double? _No11 = null;
        public double? No11
        {
            get { return this._No11; }
            set { this._No11 = value; PropertyHasChanged("No11"); }
        }

        private double? _Co11 = null;
        public double? Co11
        {
            get { return this._Co11; }
            set { this._Co11 = value; PropertyHasChanged("Co11"); }
        }

        private double? _No12 = null;
        public double? No12
        {
            get { return this._No12; }
            set { this._No12 = value; PropertyHasChanged("No12"); }
        }

        private double? _Co12 = null;
        public double? Co12
        {
            get { return this._Co12; }
            set { this._Co12 = value; PropertyHasChanged("Co12"); }
        }

        private int? _USRC = Session.LoginID;
        public int? USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _SoDuDauKyID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newSoDuDauKyID = -1;
        private SoDuDauKyBO()
        {
            _SoDuDauKyID = newSoDuDauKyID--;
        }

        public SoDuDauKyBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public SoDuDauKyBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static SoDuDauKyBO NewRootSoDuDauKyBO()
        {
            return DataPortal.Create<SoDuDauKyBO>(new RootCriteria());
        }

        internal static SoDuDauKyBO NewChildSoDuDauKyBO()
        {
            return DataPortal.Create<SoDuDauKyBO>(new ChildCriteria());
        }

        public static SoDuDauKyBO GetRootSoDuDauKyBO(int _SoDuDauKyID)
        {
            return DataPortal.Fetch<SoDuDauKyBO>(new RootCriteria(_SoDuDauKyID));
        }

        public static SoDuDauKyBO GetRootSoDuDauKyBOByWhere(string where)
        {
            return DataPortal.Fetch<SoDuDauKyBO>(where);
        }

        internal static SoDuDauKyBO GetChildSoDuDauKyBO(SqlDataReader dataReader)
        {
            return new SoDuDauKyBO(dataReader);
        }

        public static void DeleteSoDuDauKyBO(int _SoDuDauKyID)
        {
            DataPortal.Delete(new RootCriteria(_SoDuDauKyID));
        }

        public static void DeleteSoDuDauKyBO(int _SoDuDauKyID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_SoDuDauKyID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int SoDuDauKyID;
            public bool directDelete = false;
            public RootCriteria(int _SoDuDauKyID)
            {
                this.SoDuDauKyID = _SoDuDauKyID;
                this.directDelete = false;
            }

            public RootCriteria(int _SoDuDauKyID, bool directDelete)
            {
                this.SoDuDauKyID = _SoDuDauKyID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int SoDuDauKyID;
            public bool directDelete = false;
            public ChildCriteria(int _SoDuDauKyID)
            {
                this.SoDuDauKyID = _SoDuDauKyID;
                this.directDelete = false;
            }

            public ChildCriteria(int _SoDuDauKyID, bool directDelete)
            {
                this.SoDuDauKyID = _SoDuDauKyID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("SoDuDauKyBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[SoDuDauKyID] = " + criteria.SoDuDauKyID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("SoDuDauKyBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["SoDuDauKyID"] is DBNull))
                this._SoDuDauKyID = (int)dataReader["SoDuDauKyID"];
            if (!(dataReader["KhachHangID"] is DBNull))
                this._KhachHangID = (int)dataReader["KhachHangID"];
            if (!(dataReader["Nam"] is DBNull))
                this._Nam = (int)dataReader["Nam"];
            if (!(dataReader["No1"] is DBNull))
                this._No1 = (double)dataReader["No1"];
            if (!(dataReader["Co1"] is DBNull))
                this._Co1 = (double)dataReader["Co1"];
            if (!(dataReader["No2"] is DBNull))
                this._No2 = (double)dataReader["No2"];
            if (!(dataReader["Co2"] is DBNull))
                this._Co2 = (double)dataReader["Co2"];
            if (!(dataReader["No3"] is DBNull))
                this._No3 = (double)dataReader["No3"];
            if (!(dataReader["Co3"] is DBNull))
                this._Co3 = (double)dataReader["Co3"];
            if (!(dataReader["No4"] is DBNull))
                this._No4 = (double)dataReader["No4"];
            if (!(dataReader["Co4"] is DBNull))
                this._Co4 = (double)dataReader["Co4"];
            if (!(dataReader["No5"] is DBNull))
                this._No5 = (double)dataReader["No5"];
            if (!(dataReader["Co5"] is DBNull))
                this._Co5 = (double)dataReader["Co5"];
            if (!(dataReader["No6"] is DBNull))
                this._No6 = (double)dataReader["No6"];
            if (!(dataReader["Co6"] is DBNull))
                this._Co6 = (double)dataReader["Co6"];
            if (!(dataReader["No7"] is DBNull))
                this._No7 = (double)dataReader["No7"];
            if (!(dataReader["Co7"] is DBNull))
                this._Co7 = (double)dataReader["Co7"];
            if (!(dataReader["No8"] is DBNull))
                this._No8 = (double)dataReader["No8"];
            if (!(dataReader["Co8"] is DBNull))
                this._Co8 = (double)dataReader["Co8"];
            if (!(dataReader["No9"] is DBNull))
                this._No9 = (double)dataReader["No9"];
            if (!(dataReader["Co9"] is DBNull))
                this._Co9 = (double)dataReader["Co9"];
            if (!(dataReader["No10"] is DBNull))
                this._No10 = (double)dataReader["No10"];
            if (!(dataReader["Co10"] is DBNull))
                this._Co10 = (double)dataReader["Co10"];
            if (!(dataReader["No11"] is DBNull))
                this._No11 = (double)dataReader["No11"];
            if (!(dataReader["Co11"] is DBNull))
                this._Co11 = (double)dataReader["Co11"];
            if (!(dataReader["No12"] is DBNull))
                this._No12 = (double)dataReader["No12"];
            if (!(dataReader["Co12"] is DBNull))
                this._Co12 = (double)dataReader["Co12"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

            GetOldData();
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("SoDuDauKyBO_InsertBO");
            db.AddInParameter(cmd, "@KhachHangID", SqlDbType.Int, this._KhachHangID);
            db.AddInParameter(cmd, "@Nam", SqlDbType.Int, this._Nam);
            db.AddInParameter(cmd, "@No1", SqlDbType.Float, this._No1);
            db.AddInParameter(cmd, "@Co1", SqlDbType.Float, this._Co1);
            db.AddInParameter(cmd, "@No2", SqlDbType.Float, this._No2);
            db.AddInParameter(cmd, "@Co2", SqlDbType.Float, this._Co2);
            db.AddInParameter(cmd, "@No3", SqlDbType.Float, this._No3);
            db.AddInParameter(cmd, "@Co3", SqlDbType.Float, this._Co3);
            db.AddInParameter(cmd, "@No4", SqlDbType.Float, this._No4);
            db.AddInParameter(cmd, "@Co4", SqlDbType.Float, this._Co4);
            db.AddInParameter(cmd, "@No5", SqlDbType.Float, this._No5);
            db.AddInParameter(cmd, "@Co5", SqlDbType.Float, this._Co5);
            db.AddInParameter(cmd, "@No6", SqlDbType.Float, this._No6);
            db.AddInParameter(cmd, "@Co6", SqlDbType.Float, this._Co6);
            db.AddInParameter(cmd, "@No7", SqlDbType.Float, this._No7);
            db.AddInParameter(cmd, "@Co7", SqlDbType.Float, this._Co7);
            db.AddInParameter(cmd, "@No8", SqlDbType.Float, this._No8);
            db.AddInParameter(cmd, "@Co8", SqlDbType.Float, this._Co8);
            db.AddInParameter(cmd, "@No9", SqlDbType.Float, this._No9);
            db.AddInParameter(cmd, "@Co9", SqlDbType.Float, this._Co9);
            db.AddInParameter(cmd, "@No10", SqlDbType.Float, this._No10);
            db.AddInParameter(cmd, "@Co10", SqlDbType.Float, this._Co10);
            db.AddInParameter(cmd, "@No11", SqlDbType.Float, this._No11);
            db.AddInParameter(cmd, "@Co11", SqlDbType.Float, this._Co11);
            db.AddInParameter(cmd, "@No12", SqlDbType.Float, this._No12);
            db.AddInParameter(cmd, "@Co12", SqlDbType.Float, this._Co12);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewSoDuDauKyID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._SoDuDauKyID = (int)cmd.Parameters["@NewSoDuDauKyID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("SoDuDauKyBO_UpdateBO");
            db.AddInParameter(cmd, "@SoDuDauKyID", SqlDbType.Int, this._SoDuDauKyID);
            db.AddInParameter(cmd, "@KhachHangID", SqlDbType.Int, this._KhachHangID);
            db.AddInParameter(cmd, "@Nam", SqlDbType.Int, this._Nam);
            db.AddInParameter(cmd, "@No1", SqlDbType.Float, this._No1);
            db.AddInParameter(cmd, "@Co1", SqlDbType.Float, this._Co1);
            db.AddInParameter(cmd, "@No2", SqlDbType.Float, this._No2);
            db.AddInParameter(cmd, "@Co2", SqlDbType.Float, this._Co2);
            db.AddInParameter(cmd, "@No3", SqlDbType.Float, this._No3);
            db.AddInParameter(cmd, "@Co3", SqlDbType.Float, this._Co3);
            db.AddInParameter(cmd, "@No4", SqlDbType.Float, this._No4);
            db.AddInParameter(cmd, "@Co4", SqlDbType.Float, this._Co4);
            db.AddInParameter(cmd, "@No5", SqlDbType.Float, this._No5);
            db.AddInParameter(cmd, "@Co5", SqlDbType.Float, this._Co5);
            db.AddInParameter(cmd, "@No6", SqlDbType.Float, this._No6);
            db.AddInParameter(cmd, "@Co6", SqlDbType.Float, this._Co6);
            db.AddInParameter(cmd, "@No7", SqlDbType.Float, this._No7);
            db.AddInParameter(cmd, "@Co7", SqlDbType.Float, this._Co7);
            db.AddInParameter(cmd, "@No8", SqlDbType.Float, this._No8);
            db.AddInParameter(cmd, "@Co8", SqlDbType.Float, this._Co8);
            db.AddInParameter(cmd, "@No9", SqlDbType.Float, this._No9);
            db.AddInParameter(cmd, "@Co9", SqlDbType.Float, this._Co9);
            db.AddInParameter(cmd, "@No10", SqlDbType.Float, this._No10);
            db.AddInParameter(cmd, "@Co10", SqlDbType.Float, this._Co10);
            db.AddInParameter(cmd, "@No11", SqlDbType.Float, this._No11);
            db.AddInParameter(cmd, "@Co11", SqlDbType.Float, this._Co11);
            db.AddInParameter(cmd, "@No12", SqlDbType.Float, this._No12);
            db.AddInParameter(cmd, "@Co12", SqlDbType.Float, this._Co12);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_SoDuDauKyID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("SoDuDauKyBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@SoDuDauKyID", SqlDbType.Int, criteria.SoDuDauKyID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        private void GetOldData()
        {
            Type t = this.GetType();
            MemberInfo[] mi = t.GetMembers();
            int length = mi.Length;
            string name = "";
            object objValue = null;
            string strValue = null;

            for (int i = 0; i < length; i++)
                if (mi[i].MemberType == MemberTypes.Property)
                {
                    name = mi[i].Name;
                    if (StaticUtil.Log_ValidColumn(name))
                    {
                        objValue = t.GetProperty(name).GetValue(this, null);
                        if (objValue != null)
                            strValue = objValue.ToString();
                        else
                            strValue = "";

                        hstOldValue.Add(name, strValue);
                    }
                }
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_SoDuDauKyID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
